package com.zero.lite.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zero.lite.admin.annotation.LoginAdmin;
import com.zero.lite.dao.entity.Category;
import com.zero.lite.dao.service.ICategoryService;
import com.zero.lite.dao.utils.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    private final Log logger = LogFactory.getLog(CategoryController.class);

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String id, String name,
                       Page page){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        EntityWrapper<Category> ew=new EntityWrapper<>();
        Page<Category> data=categoryService.selectPage(page,ew);

        return ResponseUtil.PageOk(data);
    }

    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Category category){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        categoryService.insert(category);
        return ResponseUtil.ok();
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        Category category = categoryService.selectById(id);
        return ResponseUtil.ok(category);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Category category){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        categoryService.updateById(category);
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Category category){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        categoryService.deleteById(category.getId());
        return ResponseUtil.ok();
    }

    @GetMapping("/l1")
    public Object catL1(@LoginAdmin Integer adminId) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }

        // 所有一级分类目录
        EntityWrapper ew=new EntityWrapper();
        ew.eq("level","L1");
        List<Category> l1CatList = categoryService.selectList(ew);
        HashMap<Integer, String> data = new HashMap<>(l1CatList.size());
        for(Category category : l1CatList){
            data.put(category.getId(), category.getName());
        }
        return ResponseUtil.ok(data);
    }

}
